home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / text / manipulation / snap164.lha / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1993-11-30  |  14.1 KB  |  636 lines

  1. #if __SASC
  2. #include "snap.h"
  3. #endif
  4.  
  5. /* Auto: make "CCEXTRA=-wq -qf"
  6. */
  7.  
  8. IMPORT BOOL Kick36;
  9.  
  10. struct Window *ControlWindow = NULL;
  11.  
  12. IMPORT struct MsgPort *Sharedport;
  13. IMPORT WORD Sharedrefs;
  14. IMPORT struct Window *SaveWin;
  15.  
  16. IMPORT LONG TopBar;
  17. IMPORT LONG LeftBar;
  18. IMPORT LONG RightBar;
  19. IMPORT LONG BottomBar;
  20. IMPORT LONG ScreenFontHeight;
  21. IMPORT struct TextFont *ScreenFont;
  22. IMPORT struct RastPort MyRP;
  23.  
  24. #define IDCMPFLAGS (MOUSEMOVE | NEWSIZE | CLOSEWINDOW | GADGETUP | \
  25.                     ACTIVEWINDOW | INACTIVEWINDOW | MOUSEBUTTONS)
  26.  
  27. UBYTE *WindowTitle = (UBYTE *)"Snap 1.64 © Mikael Karlsson & Absolut Software\0";
  28.  
  29. /* Important!
  30.  * The Inactive Image structs contains the values to be used with Kickstart >= 36.
  31.  * The Active Image structs contains the values to be used with Kickstart < 36.
  32.  * The Image structs are copied accordingly in OpenStuff().
  33.  */
  34.  
  35. struct Image InactiveDiskImage = {
  36.     0, 0,
  37.     18, 12, 2,
  38.     NULL,
  39.     0x03, 0x00,
  40.     NULL
  41. };
  42.  
  43. struct Image ActiveDiskImage = {
  44.     -1, -1,
  45.     15, 13, 1,
  46.     NULL,
  47.     0x01, 0x00,
  48.     NULL
  49. };
  50.  
  51. struct Image SelDiskImage = {
  52.     0, 0,
  53.     18, 12, 2,
  54.     NULL,
  55.     0x03, 0x00,
  56.     NULL
  57. };
  58.  
  59. #if 0
  60. struct Image DiskImage = {
  61.     0, 0,
  62.     18, 13, 1,
  63.     NULL,
  64.     0x01, 0x00,
  65.     NULL
  66. };
  67. #endif
  68.  
  69. struct Image InactiveClipImage = {
  70.     0, 0,
  71.     18, 13, 2,
  72.     NULL,
  73.     0x03, 0x00,
  74.     NULL
  75. };
  76.  
  77. struct Image ActiveClipImage = {
  78.     -1, 0,
  79.     15, 14, 1,
  80.     NULL,
  81.     0x01, 0x00,
  82.     NULL
  83. };
  84.  
  85. struct Image SelClipImage = {
  86.     0, 0,
  87.     18, 13, 2,
  88.     NULL,
  89.     0x03, 0x00,
  90.     NULL
  91. };
  92.  
  93. struct Gadget DiskGad = {
  94.     NULL,
  95.     -12, 10,
  96.     10, 12,
  97.     GADGIMAGE | GRELRIGHT,
  98.     RIGHTBORDER | RELVERIFY,
  99.     BOOLGADGET,
  100.     (APTR)&InactiveDiskImage,
  101.     (APTR)&SelDiskImage,
  102.     NULL,
  103.     0L,
  104.     NULL,
  105.     DISKGAD,
  106.     NULL,
  107. };
  108.  
  109. struct Gadget ClipGad = {
  110.     NULL,
  111.     -12, 10,
  112.     10, 13,
  113.     GADGIMAGE | GRELRIGHT,
  114.     RIGHTBORDER | RELVERIFY,
  115.     BOOLGADGET,
  116.     (APTR)&InactiveClipImage,
  117.     (APTR)&SelClipImage,
  118.     NULL,
  119.     0L,
  120.     NULL,
  121.     CLIPGAD,
  122.     NULL,
  123. };
  124.  
  125. struct Gadget VProp = {
  126.     NULL,
  127.     -12, 10,
  128.     10, -15,
  129.     GADGHCOMP | GRELRIGHT | GRELHEIGHT,
  130.     RIGHTBORDER | GADGIMMEDIATE | FOLLOWMOUSE | RELVERIFY,
  131.     PROPGADGET,
  132.     NULL,
  133.     NULL,
  134.     NULL,
  135.     0L,
  136.     NULL,
  137.     VPROP,
  138.     NULL
  139. };
  140.  
  141. struct Gadget HProp = {
  142.     NULL,
  143.     4, -6,
  144.     -23, 5,
  145.     GADGHCOMP | GRELBOTTOM | GRELWIDTH,
  146.     BOTTOMBORDER | GADGIMMEDIATE | FOLLOWMOUSE | RELVERIFY,
  147.     PROPGADGET,
  148.     NULL,
  149.     NULL,
  150.     NULL,
  151.     0L,
  152.     NULL,
  153.     HPROP,
  154.     NULL
  155. };
  156.  
  157. struct PropInfo VInfo = {
  158.     AUTOKNOB | FREEVERT | PROPBORDERLESS,
  159.     0, 0,
  160.     MAXBODY, MAXBODY,
  161.     0, 0, 0, 0, 0, 0
  162. };
  163.  
  164. struct PropInfo HInfo = {
  165.     AUTOKNOB | FREEHORIZ | PROPBORDERLESS,
  166.     0, 0,
  167.     MAXBODY, MAXBODY,
  168.     0, 0, 0, 0, 0, 0
  169. };
  170.  
  171. struct Image VImage = {
  172.     0, 0, 0, 0, 0, NULL, 0, 0, NULL
  173. };
  174. struct Image HImage = {
  175.     0, 0, 0, 0, 0, NULL, 0, 0, NULL
  176. };
  177.  
  178. /* Window structure for snapped gfx */
  179. struct NewWindow Nw = {
  180.     0, 1,                   /* LeftEdge, TopEdge  */
  181.     0, 0,                   /* Width, Height */
  182.     -1, -1,                 /* DetailPen, BlockPen */
  183.     NULL,                   /* IDCMPFlags */
  184.     WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | WINDOWSIZING | RMBTRAP,
  185.     NULL, NULL,             /* FirstGadget, CheckMark */
  186.     NULL,
  187.     NULL, NULL,             /* Screen, BitMap */
  188.     0, 0,                   /* MinWidth, MinHeight */
  189.     0, 0,                   /* MaxWidth, MaxHeight */
  190.     WBENCHSCREEN            /* Type */
  191. };
  192.  
  193. SHORT Coords3[5][2] = {
  194.     {  0,  0},
  195.     { 74,  0},
  196.     { 74, 10},
  197.     {  0, 10},
  198.     {  0,  0}
  199. };
  200.  
  201. SHORT Coords2[5][2] = {
  202.     {  0,  0},
  203.     { 74,  0},
  204.     { 74, 10},
  205.     {  0, 10},
  206.     {  0,  0}
  207. };
  208.  
  209. SHORT Coords1[5][2] = {
  210.     {  0,  0},
  211.     { 34,  0},
  212.     { 34, 10},
  213.     {  0, 10},
  214.     {  0,  0}
  215. };
  216.  
  217. struct Border Border3 = {
  218.     -1, -2,
  219.     1, 0,
  220.     JAM1,
  221.     5,
  222.     (SHORT *)&Coords3,
  223.     NULL
  224. };
  225.  
  226. struct Border Border2 = {
  227.     -1, -1,
  228.     1, 0,
  229.     JAM1,
  230.     5,
  231.     (SHORT *)&Coords2,
  232.     NULL
  233. };
  234.  
  235. struct Border Border1 = {
  236.     -1, -1,
  237.     1, 0,
  238.     JAM1,
  239.     5,
  240.     (SHORT *)&Coords1,
  241.     NULL
  242. };
  243.  
  244. UBYTE TranspBuf[5];
  245.  
  246. struct StringInfo TranspSI = {
  247.     TranspBuf,  /* Buf */
  248.     NULL,       /* UndoBuf */
  249.     0,          /* BufferPos */
  250.     4,          /* MaxChars */
  251.     0,          /* DispPos */
  252.     0,          /* UndoPos */
  253.     0,          /* NumChars */
  254.     0,          /* DispCount */
  255.     0, 0,       /* CLeft, CTop */
  256.     NULL,       /* Layer */
  257.     0L,         /* LongInt */
  258.     NULL        /* AltKeyMap */
  259. };
  260.  
  261. struct Gadget TranspGad = {
  262.     NULL,
  263.     5, 16,
  264.     32, 8,
  265.     LONGINT,
  266.     RELVERIFY,
  267.     STRGADGET,
  268.     (APTR)&Border3,
  269.     NULL,
  270.     NULL,
  271.     0L,
  272.     (APTR)&TranspSI,
  273.     0,
  274.     NULL,
  275. };
  276.  
  277. IMPORT char SaveName[DSIZE+FCHARS+2];
  278.  
  279. struct StringInfo NameSI = {
  280.     (UBYTE *)SaveName,      /* Buf */
  281.     NULL,                   /* UndoBuf */
  282.     0,                      /* BufferPos */
  283.     DSIZE+FCHARS,           /* MaxChars */
  284.     0,                      /* DispPos */
  285.     0,                      /* UndoPos */
  286.     0,                      /* NumChars */
  287.     0,                      /* DispCount */
  288.     0, 0,                   /* CLeft, CTop */
  289.     NULL,                   /* Layer */
  290.     0L,                     /* LongInt */
  291.     NULL                    /* AltKeyMap */
  292. };
  293.  
  294. struct Gadget NameGad = {
  295.     &TranspGad,
  296.     5, 16,
  297.     32, 8,
  298.     NULL,
  299.     NULL,
  300.     NULL,
  301.     (APTR)&Border2,
  302.     NULL,
  303.     NULL,
  304.     0L,
  305.     NULL,
  306.     NAMEGAD,
  307.     NULL,
  308. };
  309.  
  310. struct Gadget SaveGad = {
  311.     &NameGad,
  312.     5, 16,
  313.     32, 8,
  314.     GADGHCOMP,
  315.     RELVERIFY,
  316.     BOOLGADGET,
  317.     (APTR)&Border1,
  318.     NULL,
  319.     NULL,
  320.     0L,
  321.     NULL,
  322.     SAVEGAD,
  323.     NULL,
  324. };
  325.  
  326. /* Window structure for control window */
  327. struct NewWindow NewCW = {
  328.     10, 10,           /* LeftEdge, TopEdge  */
  329.     326, 40,          /* Width, Height */
  330.     -1, -1,           /* DetailPen, BlockPen */
  331.     NULL,             /* IDCMPFlags */
  332.     WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | ACTIVATE |
  333.       SMART_REFRESH | NOCAREREFRESH | RMBTRAP,
  334.     NULL, NULL,       /* FirstGadget, CheckMark */
  335.     (UBYTE *)"Snap Control Window",
  336.     NULL, NULL,       /* Screen, BitMap */
  337.     32, 32,           /* MinWidth, MinHeight */
  338.     -1, -1,           /* MaxWidth, MaxHeight */
  339.     WBENCHSCREEN      /* Type */
  340. };
  341.  
  342. #ifdef REQLIB
  343. IMPORT struct ReqFileRequester *NameFR;
  344. #endif REQLIB
  345. #ifdef ASLLIB
  346. IMPORT struct FileRequester *AslNameFR;
  347. #endif ASLLIB
  348.  
  349. struct Window *opensharedwindow(nw)
  350. struct NewWindow *nw;
  351. {
  352.     struct Window *win;
  353.     struct Screen scr;
  354.  
  355.     if (!GetScreenData((char *)&scr, (LONG)sizeof(struct Screen),
  356.       WBENCHSCREEN, NULL)) {
  357.         return NULL;     /* No WB */
  358.     }
  359.     if (nw->TopEdge+nw->Height > scr.Height) {
  360.         nw->TopEdge = scr.Height-nw->Height;
  361.     }
  362.     if (nw->LeftEdge+nw->Width > scr.Width) {
  363.         nw->LeftEdge = scr.Width-nw->Width;
  364.     }
  365.     if (nw->TopEdge < 0) {
  366.         nw->TopEdge = 0;
  367.     }
  368.     if (nw->LeftEdge < 0) {
  369.         nw->LeftEdge = 0;
  370.     }
  371.     if (nw->TopEdge+nw->Height > scr.Height) {
  372.         nw->Height = scr.Height-nw->TopEdge;
  373.     }
  374.     if (nw->LeftEdge+nw->Width > scr.Width) {
  375.         nw->Width = scr.Width-nw->LeftEdge;
  376.     }
  377.  
  378.     nw->IDCMPFlags = NULL;
  379.  
  380.     if (win = OpenWindow(nw)) {
  381.         if (!Sharedport && !(Sharedport = CreatePort(0L, 0L))) {
  382.             return NULL;
  383.         }
  384.         win->UserPort = Sharedport;
  385.         ModifyIDCMP(win, IDCMPFLAGS);
  386.         ++Sharedrefs;
  387.     }
  388.     return(win);
  389. }
  390.  
  391.  
  392. VOID closesharedwindow(win)
  393. struct Window *win;
  394. {
  395.     REGISTER struct IntuiMessage *im, *safe;
  396.     Forbid();
  397.     Sharedrefs--;
  398.     SAFEMAPLIST(&win->UserPort->mp_MsgList,
  399.       struct IntuiMessage *, im, safe) {
  400.         if (im->IDCMPWindow == win) {
  401.             Remove((struct Node *)im);
  402.             ReplyMsg((struct Message *)im);
  403.         }
  404.     }
  405.     win->UserPort = NULL;
  406.     ModifyIDCMP(win, NULL);
  407.     Permit();
  408.     CloseWindow(win);
  409.     if (!Sharedrefs) {
  410.         DeletePort(Sharedport);
  411.         Sharedport = NULL;
  412.     }
  413. }
  414.  
  415. VOID SetUpBorder(Border, Gad)
  416. struct Border *Border;
  417. struct Gadget *Gad;
  418. {
  419.     Border->XY[2] = Border->XY[4] = Gad->Width + 1;
  420.     Border->XY[5] = Border->XY[7] = Gad->Height + 1;
  421. }
  422.  
  423. VOID DrawOldBorders(GS)
  424. struct GfxSnap *GS;
  425. {
  426.     REGISTER LONG right;
  427.     REGISTER LONG bottom;
  428.  
  429.     if (!Kick36) {
  430.         right = GS->window->Width - GS->rightbar;
  431.         bottom = GS->window->Height - GS->bottombar;
  432.         SetDrMd(GS->window->RPort, JAM1);
  433.         SetAPen(GS->window->RPort, 0L);
  434.         Move(GS->window->RPort, 2L,        bottom);
  435.         Draw(GS->window->RPort, right,     bottom);
  436.         Move(GS->window->RPort, 2L,        bottom + 2);
  437.         Draw(GS->window->RPort, right,     bottom + 2);
  438.         Move(GS->window->RPort, right,     (LONG)GS->topbar);
  439.         Draw(GS->window->RPort, right,     bottom);
  440.         Draw(GS->window->RPort, right + 1, bottom);
  441.         Draw(GS->window->RPort, right + 1, (LONG)GS->topbar);
  442.         SetAPen(GS->window->RPort, 1L);
  443.         Move(GS->window->RPort, right + 2, (LONG)GS->topbar);
  444.         Draw(GS->window->RPort, right + 2, bottom);
  445.         Draw(GS->window->RPort, right + 3, bottom);
  446.         Draw(GS->window->RPort, right + 3, (LONG)GS->topbar);
  447.         Move(GS->window->RPort, 2L,        bottom + 1);
  448.         Draw(GS->window->RPort, right + 1, bottom + 1);
  449.     }
  450. }
  451.  
  452. VOID AdjustSize(GS)
  453. struct GfxSnap *GS;
  454. {
  455.     ULONG w = GS->window->Width - GS->leftbar - GS->rightbar;
  456.     ULONG h = GS->window->Height - GS->topbar - GS->bottombar;
  457.     ULONG vb, hb;
  458.  
  459.     vb = hb = MAXBODY;
  460.  
  461.     if (w < GS->width) {
  462.         hb = w * (ULONG)MAXBODY / GS->width;
  463.     }
  464.     if (h < GS->height) {
  465.         vb = h * (ULONG)MAXBODY / GS->height;
  466.     }
  467.     NewModifyProp(&GS->VProp, GS->window, NULL,
  468.       (ULONG)AUTOKNOB | FREEVERT | PROPBORDERLESS,
  469.       0L, 0L, (ULONG)MAXBODY, vb, 1L);
  470.     NewModifyProp(&GS->HProp, GS->window, NULL,
  471.       (ULONG)AUTOKNOB | FREEHORIZ | PROPBORDERLESS,
  472.       0L, 0L, hb, (ULONG)MAXBODY, 1L);
  473.     RefreshWindowFrame(GS->window);
  474.     DrawOldBorders(GS);
  475.     InitRastPort(&MyRP);
  476.     MyRP.BitMap = &GS->BM;
  477.     SetAPen(GS->window->RPort, 0);
  478.     RectFill(GS->window->RPort,
  479.       (LONG)GS->leftbar, (LONG)GS->topbar,
  480.       (LONG)GS->leftbar + w - 1, (LONG)GS->topbar + h - 1);
  481.     if (w > GS->width)  w = GS->width;
  482.     if (h > GS->height) h = GS->height;
  483.     ClipBlit(&MyRP, 0L, 0L, GS->window->RPort,
  484.       (LONG)GS->leftbar, (LONG)GS->topbar, w, h, 0xC0L);
  485. }
  486.  
  487. VOID SyncGS(GS)
  488. struct GfxSnap *GS;
  489. {
  490.     ULONG w = GS->window->Width - GS->leftbar - GS->rightbar;
  491.     ULONG h = GS->window->Height - GS->topbar - GS->bottombar;
  492.     ULONG vpos, hpos;
  493.     LONG temp;
  494.  
  495.     temp = GS->width - w;
  496.     if (temp <= 0) {
  497.         hpos = 0;
  498.     } else {
  499.         hpos = temp * (LONG)GS->HInfo.HorizPot / MAXBODY;
  500.         if (hpos > temp) {
  501.             hpos = temp;
  502.         }
  503.     }
  504.     temp = GS->height - h;
  505.     if (temp <= 0) {
  506.         vpos = 0;
  507.     } else {
  508.         vpos = temp * (LONG)GS->VInfo.VertPot / MAXBODY;
  509.         if (vpos > temp) {
  510.             vpos = temp;
  511.         }
  512.     }
  513.     DrawOldBorders(GS);
  514.     InitRastPort(&MyRP);
  515.     MyRP.BitMap = &GS->BM;
  516.     if (w > GS->width) {
  517.         w = GS->width;
  518.     }
  519.     if (h > GS->height) {
  520.         h = GS->height;
  521.     }
  522.     ClipBlit(&MyRP, hpos, vpos, GS->window->RPort,
  523.       (LONG)GS->leftbar, (LONG)GS->topbar, w, h, 0xC0L);
  524. }
  525.  
  526. SHORT OpenCW()
  527. {
  528.     WORD temp;
  529.     struct RastPort TmpRP;
  530.  
  531.     FixHeights();
  532.  
  533.     InitRastPort(&TmpRP);
  534.     SetFont(&TmpRP, ScreenFont);
  535.  
  536.     SaveGad.TopEdge = NameGad.TopEdge = TopBar + 2;
  537.     SaveGad.Height = NameGad.Height = TranspGad.Height = ScreenFontHeight + 2;
  538.  
  539.     TranspGad.TopEdge = SaveGad.TopEdge + SaveGad.Height + 5;
  540.  
  541.     SaveGad.LeftEdge = LeftBar + 2;
  542.     SaveGad.Width = TextLength(&TmpRP, "Save", 4L) + 1;
  543.     SaveGad.Flags |= GADGDISABLED;
  544.  
  545.     NameGad.LeftEdge = SaveGad.LeftEdge +
  546.       TextLength(&TmpRP, "Save as ", 8L);
  547.  
  548.     temp = 322 - NameGad.LeftEdge;
  549.     NameGad.Width =
  550.       temp - (temp % TextLength(&TmpRP, " ", 1L)) + 1;
  551.  
  552.     TranspGad.LeftEdge = SaveGad.LeftEdge +
  553.       TextLength(&TmpRP, "Transparent color ", 18L);
  554.     TranspGad.Width = TextLength(&TmpRP, "0000", 4L) + 2;
  555.  
  556.     NewCW.Height = TopBar + BottomBar + ScreenFontHeight * 2 + 12;
  557.  
  558. #define dmax(a,b) ((a>b)?a:b)
  559.     NewCW.Width=dmax((TranspGad.LeftEdge + TranspGad.Width + 2 + RightBar), (NameGad.LeftEdge + NameGad.Width + 2 + RightBar));
  560. #undef dmax
  561.  
  562.     ControlWindow = opensharedwindow(&NewCW);
  563.     if (!ControlWindow) {
  564.         return 0;
  565.     }
  566.     SetFont(ControlWindow->RPort, ScreenFont);
  567.     ModifyIDCMP(ControlWindow, ControlWindow->IDCMPFlags&~NEWSIZE);
  568.  
  569.     SetUpBorder(&Border1, &SaveGad);
  570.     SetUpBorder(&Border2, &NameGad);
  571.     SetUpBorder(&Border3, &TranspGad);
  572.  
  573. #if defined(REQLIB) || defined(ASLLIB)
  574. #ifdef REQLIB
  575. #ifdef ASLLIB
  576.     if ((NameFR) || (AslNameFR))
  577. #else ASLLIB
  578.     if (NameFR)
  579. #endif ASLLIB
  580. #else
  581.     if (AslNameFR)
  582. #endif REQLIB
  583.     {
  584.         NameGad.Flags = GADGHCOMP;
  585.         NameGad.Activation = RELVERIFY;
  586.         NameGad.GadgetType = BOOLGADGET;
  587.         NameGad.SpecialInfo = NULL;
  588.     } else
  589. #endif REQLIB || ASLLIB
  590.     {
  591.         NameGad.Flags = SELECTED;
  592.         NameGad.Activation = NULL;
  593.         NameGad.GadgetType = STRGADGET;
  594.         NameGad.SpecialInfo = (APTR)&NameSI;
  595.         ++NameGad.LeftEdge;         /* Adjust string gadget position */
  596.         ++NameGad.TopEdge;          /* Adjust string gadget position */
  597.     }
  598.  
  599.     strcpy(SaveName, "Snap.pic");
  600.     strcpy((char *)TranspBuf, "0");
  601.     TranspSI.LongInt = 0L;
  602.  
  603.     SetAPen(ControlWindow->RPort, 1L);
  604.     SetBPen(ControlWindow->RPort, 0L);
  605.     SetDrMd(ControlWindow->RPort, JAM2);
  606.     Move(ControlWindow->RPort,
  607.       (LONG)SaveGad.LeftEdge + 1,
  608.       (LONG)SaveGad.TopEdge + ControlWindow->RPort->Font->tf_Baseline + 1);
  609.     Text(ControlWindow->RPort, "Save as", 7L);
  610.     Move(ControlWindow->RPort,
  611.       (LONG)SaveGad.LeftEdge + 1,
  612.       (LONG)TranspGad.TopEdge + ControlWindow->RPort->Font->tf_Baseline);
  613.     Text(ControlWindow->RPort, "Transparent color", 17L);
  614.     AddGList(ControlWindow, &SaveGad, -1L, 3L, NULL);
  615.     RefreshGList(&SaveGad, ControlWindow, NULL, 3L);
  616.  
  617. #if defined(REQLIB) || defined(ASLLIB)
  618. #ifdef REQLIB
  619. #ifdef ASLLIB
  620.     if ((NameFR) || (AslNameFR))
  621. #else  ASLLIB
  622.     if (NameFR)
  623. #endif ASLLIB
  624. #else
  625.     if (AslNameFR)
  626. #endif REQLIB
  627.     {
  628.         GadText(&NameGad, SaveName, 8L);
  629.     } else
  630. #endif REQLIB || ASLLIB
  631.     {
  632.         ActivateGadget(&NameGad, ControlWindow, NULL);
  633.     }
  634.     return 1;
  635. }
  636.